package com.sgai.zero_robot.core;

import static com.sgai.zero_robot.core.Config.eight;
import static com.sgai.zero_robot.core.Config.eighteen;
import static com.sgai.zero_robot.core.Config.eleven;
import static com.sgai.zero_robot.core.Config.fifteen;
import static com.sgai.zero_robot.core.Config.five;
import static com.sgai.zero_robot.core.Config.four;
import static com.sgai.zero_robot.core.Config.fourteen;
import static com.sgai.zero_robot.core.Config.nine;
import static com.sgai.zero_robot.core.Config.nineteen;
import static com.sgai.zero_robot.core.Config.one;
import static com.sgai.zero_robot.core.Config.seven;
import static com.sgai.zero_robot.core.Config.seventeen;
import static com.sgai.zero_robot.core.Config.six;
import static com.sgai.zero_robot.core.Config.sixteen;
import static com.sgai.zero_robot.core.Config.ten;
import static com.sgai.zero_robot.core.Config.three;
import static com.sgai.zero_robot.core.Config.threeteen;
import static com.sgai.zero_robot.core.Config.twelve;
import static com.sgai.zero_robot.core.Config.twenty;
import static com.sgai.zero_robot.core.Config.two;

import com.sgai.zero_robot.pojo.AlarmInfo;

public class Protocol {

    /**
     * 解析收到的数据帧
     */
    public static byte[] handleFrame(byte[] data) {
//        if (data.length < 5) return;
//
//        if (data[0] == (byte) 0xAA && data[1] == (byte) 0x55) {
//            int len = data[2] & 0xFF;
//            if (data.length >= len + 4) {
//                byte[] payload = new byte[len];
//                System.arraycopy(data, 3, payload, 0, len);
//                byte checksum = data[len + 3];
//
//                if (verifyChecksum(payload, checksum)) {
//                    System.out.println("⬅️ 收到有效数据: " + bytesToHex(payload));
//                    // TODO: 根据 Excel 协议文档解析 payload
//                } else {
//                    System.err.println("❌ 校验错误: " + bytesToHex(data));
//                }
//            }
//        }
        return data;
    }

    /**
     * 封装发送帧
     */
    public static byte[] buildFrame(byte[] payload) {
//        int len = payload.length;
//        byte checksum = calcChecksum(payload);
//
//        byte[] frame = new byte[len + 4];
//        frame[0] = (byte) 0xAA;
//        frame[1] = (byte) 0x55;
//        frame[2] = (byte) len;
//        System.arraycopy(payload, 0, frame, 3, len);
//        frame[len + 3] = checksum;
//        return frame;
        return payload;
    }

    /**
     * 校验和
     */
    private static boolean verifyChecksum(byte[] data, byte checksum) {
        return calcChecksum(data) == checksum;
    }

    private static byte calcChecksum(byte[] data) {
        byte sum = 0;
        for (byte b : data) sum += b;
        return sum;
    }

    /**
     * 工具：字节数组转 HEX
     */
    public static String bytesToHex(byte[] bytes) {
        StringBuilder sb = new StringBuilder();
        for (byte b : bytes) sb.append(String.format("%02X ", b));
        return sb.toString();
    }
    public static  AlarmInfo getAlarmInfo(String value, String stringValue, String message) {
        String key = null;
        String alarm = null;
        if (one.equals(value)) {
            alarm = "遥控器sbus通讯异常";
            key = one;
        } else if (two.equals(value)) {
            alarm = "左侧行走电机通讯异常";
            key = two;
        } else if (three.equals(value)) {
            alarm = "左侧行走电机故障";
            key = three;
        } else if (four.equals(value)) {
            alarm = "前侧摆动电机通讯异常";
            key = four;
        } else if (five.equals(value)) {
            alarm = "前侧摆动电机故障";
            key = five;
        } else if (six.equals(value)) {
            alarm = "后侧摆动电机通讯异常";
            key = six;
        } else if (seven.equals(value)) {
            alarm = "后侧摆动电机故障";
            key = seven;
        } else if (eight.equals(value)) {
            alarm = "右侧行走电机通讯异常";
            key = eight;
        } else if (nine.equals(value)) {
            alarm = "右侧行走电机故障";
            key = nine;
        } else if (ten.equals(value)) {
            alarm = "前侧激光测距通讯异常";
            key = ten;
        } else if (eleven.equals(value)) {
            alarm = "前侧激光测距数据越界";
            key = eleven;
        } else if (twelve.equals(value)) {
            alarm = "后侧激光测距通讯异常";
            key = twelve;
        } else if (threeteen.equals(value)) {
            alarm = "后侧激光测距数据越界";
            key = threeteen;
        } else if (fourteen.equals(value)) {
            alarm = "惯导模块初始化异常";
            key = fourteen;
        } else if (fifteen.equals(value)) {
            alarm = "惯导模块感知机器人位置越界";
            key = fifteen;
        } else if (sixteen.equals(value)) {
            alarm = "惯导模块通讯异常";
            key = sixteen;
        } else if (seventeen.equals(value)) {
            alarm = "前侧光感模块初始化异常";
            key = seventeen;
        } else if (eighteen.equals(value)) {
            alarm = "后侧光感模块初始化异常";
            key = eighteen;
        } else if (nineteen.equals(value)) {
            alarm = "电池通讯异常";
            key = nineteen;
        } else if (twenty.equals(value)) {
            alarm = "急停报警";
            key = twenty;
        } else if ("21".equals(value)) {
            alarm = "硬件重启";
            key = "21";
        } else if ("22".equals(value)) {
            alarm = "惯导模块感知机器人俯仰越界";
            key = "22";
        } else if ("23".equals(value)) {
            alarm = "不满足自动检测的条件";
            key = "23";
        }

        else {
            alarm = "未知";
            key = value;
        }
        return new AlarmInfo(alarm, key, one.equals(stringValue),System.currentTimeMillis(),message);
    }

    public static String getInfo(String infoValue) {
        if (one.equals(infoValue)){
            return "探针不垂直地面，不允许行走";
        }else{
            return "未知异常";
        }
    }
}
